package com.example.bankend.controller;

import com.example.bankend.model.Customer;
import com.example.bankend.model.User;
import com.example.bankend.service.method.UserCustomerService;
import com.example.bankend.service.method.UserServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Api(tags="用户管理")
@RestController
@RequestMapping("/api/auth")
public class AuthController {

    private final UserServiceImpl userService;



    @Autowired
    public AuthController(UserServiceImpl userService) {
        this.userService = userService;
    }

    @Autowired
    private UserCustomerService userCustomerService;

    @ApiOperation(value = "用户测试",notes = "用户测试notes")
    @GetMapping("/test")  //测试接口
    public String test() {
        return "Hello, World!";
    }
    //注册接口
    @ApiOperation(value = "用户注册")
    @PostMapping("/register")  //注册接口
    public ResponseEntity<?> register(@RequestBody User user) {
        try {
            // 检查用户名是否已存在
            User existingUser = userService.findByUsername(user.getUsername());
            if (existingUser != null) {
                throw new RuntimeException("该用户名已存在");
            }

            // 插入新用户
            userService.registerUser(user);
            return ResponseEntity.status(HttpStatus.CREATED).body(user);
        } catch (RuntimeException e) {
            if (e.getMessage().equals("该用户名已存在")) {
                return ResponseEntity.status(HttpStatus.CONFLICT).body(e.getMessage());
            } else {
                return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("注册失败");
            }
        }
    }
    //登录接口
    @ApiOperation(value = "用户登录")
    @PostMapping("/login")   //验证登录接口
    public ResponseEntity<?> login(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();

        // 验证登录
        User loginUser = userService.login(username, password);
        if (loginUser != null) {
            return ResponseEntity.ok(loginUser); // 登录成功
        } else {
            // 检查用户是否已注册
            User registeredUser = userService.findByUsername(username);
            if (registeredUser == null) {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户未注册，请先注册！");
            } else {
                return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误！");
            }
        }
    }
    @ApiOperation(value = "根据用户名删除用户")
    @DeleteMapping("/delete/{username}")
    public ResponseEntity<String> deleteUser(@PathVariable String username) {
        userService.deleteuser(username);
        return ResponseEntity.ok("用户已成功删除！");
    }
    @ApiOperation(value = "根据用户名拿取用户信息")
    @GetMapping("/user/{username}")  //拿数据
    public User getUser(@PathVariable String username) {
        return userService.findByUsername(username);
    }

    // 通过用户名查询客户信息
    @ApiOperation(value = "根据客户名拿取用户信息")
    @GetMapping("/customer/{username}")
    public ResponseEntity<Customer> getCustomerByUsername(@PathVariable String username) {
        Customer customer = userCustomerService.getCustomerByUserName(username);

        if (customer != null) {
            return ResponseEntity.ok(customer);
        } else {
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);
        }
    }

}